app.controller('findpwdPageCtrl', function($scope, $interval, $cordovaCamera, PopupService, $http, $myconfig, $state, $stateParams, $ionicLoading, $mydialog, $ionicConfig) {
    //接收参数 --用户登录手机号
    $scope.finPwdMob = $stateParams.phoNum;
    // console.log($scope.finPwdMob);

    //定义倒计时
    $scope.secend = 60;

    //定义重置参数
    $scope.useMesR = {
        mobile: $scope.finPwdMob,
        // code:'',
    };
    $scope.seBtn = true;


    //如果已接收到手机则发送验证码至用户手机
    $scope.getCode = function(code) {
        if (!code) {
            var str = document.getElementById('phoneNum').value.trim();
            if (str) {
                reg = /^(13[0-9]|17[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
                if (!reg.test(str)) {
                    $scope.err = "请输入正确的手机号!"; //请将“双精度类型”要换成你要验证的那个属性名称！
                    $mydialog.fail($scope);
                    return;
                } else {
                    code = document.getElementById('phoneNum').value;
                }
            } else if (!str) {
                $scope.err = '请输入手机号';
                $mydialog.fail($scope);
                return;
            }
        }
        $http.post($myconfig.api.base_host + '/users/get_code', {
            "mobile": code,
            "code_type": "lost_password"
        }).then(function(res) {
            if (res.data.success) {
                $scope.seBtn = false;
                $interval(function() {
                    $scope.secend--
                        if ($scope.secend == 0) {
                            $scope.seBtn = true;
                            $scope.secend = 60;
                        }
                }, 1000, 60);
            } else {
                //  如果没获取到验证码，则向页面抛出后台返回错误信息
                $scope.err = res.data.data;
                $mydialog.fail($scope);
            }
        });
    };

    //如果页面已接收到手机号码 则自动执行获取验证码动作
    if ($scope.finPwdMob) {
        $scope.getCode($scope.finPwdMob);
        document.getElementById('phoneNum').value = $scope.finPwdMob;
    };


    //页面跳转
    $scope.GoEnterPwd = function() {
        if (!$scope.useMesR.mobile) {
            $scope.useMesR.mobile = document.getElementById('phoneNum').value.trim();
        }
        $http.post($myconfig.api.base_host + '/users/verify', {
            "code": $scope.useMesR.code,
            "mobile": $scope.useMesR.mobile,
            "code_type": "lost_password"
        }).then(function(res) {
            if (res.data.success) {
                if (!res.data.headimgurl) {
                    $scope.useMesR.headimgurl = '';
                } else {
                    $scope.useMesR.headimgurl = 'assets.quanminkang.com.cn/' + res.data.headimgurl;
                }
                $state.go('app.enterpwd', { mes: JSON.stringify($scope.useMesR) })
            } else {
                $scope.err = res.data.data;
                $mydialog.fail($scope);
            }
        })
    };


    $scope.$on('$ionicView.enter', function() {

        $ionicConfig.views.swipeBackEnabled(false);

    });

});
